<template>
  <div>
    <!-- 菜单信息 用于新增或者修改  应该是一个表单 -->
    <el-dialog
      :title="info.type === 'add' ? '新增' : '修改'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleid">
          <el-select
            v-model="ruleForm.roleid"
            placeholder="请选择上级目录"
          >
            <el-option
              v-for="(menu, index) in optionList"
              :key="index"
              :label="menu.rolename"
              :value="menu.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item  label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import router from '@/router';
import { addUser ,getRoleList} from "../../apis";
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        // 表单的数据
        roleid: 0,
        username: "",
        password: "",
        status: 1,
      },
      rules: {
        // 只有用户名是必须添的
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
      },
      // 下拉列表的数据
      optionList: [],
    };
  },
  created() {
    // 获取下框对应的数据
    this.getOptionList();
  },
  methods: {
    // 这个函数的作用就是给ruleForm赋值，以显示表单的内容
    setVal(val) {
      this.ruleForm = { ...val };
    },
    async getOptionList() {
      let res = await getRoleList();
      this.optionList = res.list;
    },
    submit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res;
          // 区分新增还是修改
          if (this.info.type === "add") {
            // 新增
            res = await addUser(this.ruleForm);
          } else {
            // 修改
            res = await editMenu(this.ruleForm);
          }
          if (res.code == 200) {
            // 更新成功，通知list组件重新获取数据来更新列表
            this.$emit("update");
          }
          // 关闭弹窗
          this.close();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    close() {
      this.info.isShow = false;
    },
  },
};
</script>

<style></style>
